package game.entity;

import game.util.Direction;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsq
 * @date 2023/1/15 下午8:59
 */
public class Player extends AbstractEntity{

    private Map<Direction, List<Image>> imageMap;

    private int imageIndex;

    private int liveNumber = 5;

    private Image headIcon;

    public Player(int x, int y, char c) {
        super(x, y, c);
        broken = true;
        direction = Direction.DOWN;
        imageMap = new HashMap<>();
        List<Image> downImages = new ArrayList<>();
        this.headIcon = new Image("file:src/game/image/player.png");
        downImages.add(new Image("file:src/game/image/player1.png"));
        downImages.add(new Image("file:src/game/image/player2.png"));
        downImages.add(new Image("file:src/game/image/player3.png"));
        downImages.add(new Image("file:src/game/image/player4.png"));
        imageMap.put(Direction.DOWN, downImages);

        List<Image> leftmages = new ArrayList<>();
        leftmages.add(new Image("file:src/game/image/player_left1.png"));
        leftmages.add(new Image("file:src/game/image/player_left2.png"));
        leftmages.add(new Image("file:src/game/image/player_left3.png"));
        leftmages.add(new Image("file:src/game/image/player_left4.png"));
        imageMap.put(Direction.LEFT, leftmages);

        List<Image> rightImages = new ArrayList<>();
        rightImages.add(new Image("file:src/game/image/player_right1.png"));
        rightImages.add(new Image("file:src/game/image/player_right2.png"));
        rightImages.add(new Image("file:src/game/image/player_right3.png"));
        rightImages.add(new Image("file:src/game/image/player_right4.png"));
        imageMap.put(Direction.RIGHT, rightImages);

        List<Image> upImages = new ArrayList<>();
        upImages.add(new Image("file:src/game/image/player_up1.png"));
        upImages.add(new Image("file:src/game/image/player_up2.png"));
        upImages.add(new Image("file:src/game/image/player_up3.png"));
        upImages.add(new Image("file:src/game/image/player_up4.png"));
        imageMap.put(Direction.UP, upImages);
    }

    @Override
    public void draw(GraphicsContext g, int frame) {
        int value = frame % 2;
        if (value == 0) {
            if (imageIndex < 3) {
                imageIndex ++;
            } else {
                imageIndex = 0;
            }
        }
        List<Image> images = imageMap.get(direction);
        image = images.get(imageIndex);
        super.draw(g, frame);
    }

    public int getLiveNumber() {
        return liveNumber;
    }

    public void reduceLiveNumber(){
        this.liveNumber --;
    }

    public Image getHeadIcon() {
        return headIcon;
    }
}